package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsCategoryMapper;
import com.qiguliuxing.dts.db.domain.DtsCategory;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsCategoryService {
	@Resource
	private DtsCategoryMapper categoryMapper;

	public List<DtsCategory> queryL1WithoutRecommend(int offset, int limit) {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("level", "L1");
		queryWrapper.ne("name", "推荐");
		queryWrapper.eq("deleted", false);
		IPage<DtsCategory> iPage = new Page<>(offset, limit);
		iPage = categoryMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}

	public List<DtsCategory> queryL1() {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("level", "L1");
		queryWrapper.eq("deleted", false);
		return categoryMapper.selectList(queryWrapper);
	}

	public List<DtsCategory> queryByPid(Integer pid) {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("pid", pid);
		queryWrapper.eq("deleted", false);
		return categoryMapper.selectList(queryWrapper);
	}

	public List<DtsCategory> queryL2ByIds(List<Integer> ids) {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("id", ids);
		queryWrapper.eq("level", "L2");
		queryWrapper.eq("deleted", false);
		return categoryMapper.selectList(queryWrapper);
	}

	public DtsCategory findById(Integer id) {
		return categoryMapper.selectById(id);
	}

	public List<DtsCategory> querySelective(String id, String name, Integer page, Integer size, String sort,
			String order) {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(id)) {
			queryWrapper.eq("id", id);
		}
		if (StrUtil.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		queryWrapper.eq("deleted", false);
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsCategory> iPage = new Page<>(page, size);
		iPage = categoryMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}

	public int updateById(DtsCategory category) {
		category.setUpdateTime(LocalDateTime.now());
		return categoryMapper.updateById(category);
	}

	public void deleteById(Integer id) {
		categoryMapper.deleteById(id);
	}

	public void add(DtsCategory category) {
		category.setAddTime(LocalDateTime.now());
		category.setUpdateTime(LocalDateTime.now());
		categoryMapper.insert(category);
	}

	public List<DtsCategory> queryChannel() {
		QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("level", "L1");
		queryWrapper.eq("deleted", false);
		IPage<DtsCategory> iPage = new Page<>(1, 9);
		iPage = categoryMapper.selectPage(iPage, queryWrapper);
		return iPage.getRecords();
	}
}
